package com.zzyl.serve.controller.member;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.serve.vo.ReservationCountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import kotlin.Result;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.Reservation;
import com.zzyl.serve.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 *
 * @author yanbucai
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public TableDataInfo<List<Reservation>> list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 查询预约信息列表
     */
//    @GetMapping("/cancelled-count")
//    @ApiOperation(value = "获取预约信息次数")
//    public TableDataInfo<List<Reservation>> cancelledCount(Reservation reservation)
//    {
//        //获取预约次数
//        List<Reservation> list = reservationService.selectReservationList(reservation);
//
//        return getDataTable(list);
//    }
    @GetMapping("/cancelled-count")
    public AjaxResult getTodayCancelledCount() {
        // 从线程中获取当前用户ID（根据你的鉴权框架实现）
        Long userId = UserThreadLocal.getUserId();
        return AjaxResult.success(reservationService.getTodayCancelledCount(userId));

    }



    /**
     * 导出预约信息列表
     */
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出预约信息列表")
    public void export(HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true)
            @PathVariable("id") Long id)
    {
        return R.ok(reservationService.selectReservationById(id));
    }
    /**
     * 获取预约信息详细信息
     */
    /**
     * 获取时间段容量信息
     */
    @GetMapping("/countByTime")
    public AjaxResult getTimeSlots(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime time) {
        LocalDateTime inputTime = time;
        return AjaxResult.success(reservationService.getTimeSlotInfo(inputTime));
    }




    /**
     * 新增预约信息
     */
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation)

    {
        System.out.println(reservation);
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }
}
